<?
/**
  * Reverse Geocode Class
  * v.000000001
  * By Harper Reed
  * Harper@nata2.org
  * I am pretty awesome
  * 
  * I hate documenting my code. I cannot lie. But its pretty easy. 
  * I will make more documentation later. Basically you just do this:
  * 
  *
  * -----------------------------------------------------------
  *
  * Copyright 2006 Harper Reed
  * 
  * -----------------------------------------------------------
  *
  * LICENSE
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License (GPL)
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * To read the license please visit http://www.gnu.org/copyleft/gpl.html
  *
  */

  
class Rgeocode{

	var $lat;
	var $lng;

    // Constructor:
	function Rgeocode($lat, $lng){
		$this->lat= $lat;
		$this->lng= $lng;
	}
	
	function response()
	{
		$url = "http://imaptools.com/rgeo/?y=".$this->lat."&x=".$this->lng;
		$resp = file_get_contents($url);
		$response = $this->parse($resp);
		
		return $response;
		
	}
	
	function clean($input)
	{
		$temp = strval($input);
		$temp = str_replace("\n","",$temp);
		$temp = str_replace("\r","",$temp);
		$temp = str_replace(" ","",$temp);
		return $temp;

	}
	
	function parse($resp)
	{
		$xml_resp = new SimpleXMLElement($resp);
		$f = $xml_resp->feature;
		foreach($f as $feature)
		{
		$desc= $this->clean($feature->description);
		$body= $feature->property;
		$d="";
		foreach ($body as $b)
		{	
			$temp=str_replace("  ","",strval($b));
			$temp=str_replace("\r","",$temp);
			$temp=str_replace("\n","",$temp);
			$d[] = $temp;
		}
		$body = implode(",",$d);
		$i[$desc] = $body;
		}
		
		list($c["country"],$c["capital"],$c["continent"],$y)= split(",",$i[WorldCountries]);
		list($c["state"],$y)= split(",",$i[States]);
		list($c["city"],$y,$y,$y)= split(",",$i[Place]);
		list($c["county"],$y,$y,$y)= split(",",$i[County]);
		list($y,$street,$y,$streetnum,$y)= split(",",$i[SurfaceStreets]);
		if ($streetnum!="") $c["address"] = $streetnum." ".$street;
		list($c["zip"],$y)= split(",",$i[ZipcodeTabulationArea]);
		$c[raw] = $i;
		
		return $c;
	}
 
	

}
?>
